import {
  axiosTokenProxy,
  mockAxiosDecorator
} from "../utils/reqTools";
import moment from 'moment';
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const normalAxios = axios.create();
const mockAxios = axios.create();

// mock 数据
const mock = new MockAdapter(mockAxios);


mock.onGet('/api/sysOffice').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/sysOffice/findSysOfficeByType', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        res.data.forEach(element => {
          element.value = element.id;
          element.label = element.name;
          element.isLeaf = false;
        });
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*resolve([200, {
      "code": 0,
      "data": [{
        value: '0',
        label: '小区1',
        type: 1,
        isLeaf: false,
      }, {
        value: '1',
        type: 1,
        label: '小区2',
        isLeaf: false,
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/api/office/child').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/sysOffice/findChildrenOfficeByPId', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        let ele = [];
        res.data.forEach(element => {
          if (element.type == 3) {
            element.value = element.id;
            element.label = element.name;
            element.isLeaf = false;
            ele.push(element);
          }
        });
        res.data = ele;
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*resolve([200, {
      "code": 0,
      "data": [{
        value: '0',
        label: '小区1',
        type: 1,
        isLeaf: false,
      }, {
        value: '1',
        type: 1,
        label: '小区2',
        isLeaf: false,
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/community/getCommunity').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityInfo/find_by_office_id', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        res.data.forEach(element => {
          element.value = element.id;
          element.label = element.name;
          element.isLeaf = false;
          element.createDate = moment(element.createDate).format("YYYY-MM-DD HH:mm:ss")
        });
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*resolve([200, {
      "code": 0,
      "data": [{
        value: '0',
        label: '小区1',
        type: 1,
        isLeaf: false,
      }, {
        value: '1',
        type: 1,
        label: '小区2',
        isLeaf: false,
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/community/getBuild').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructure/find_build_by_info_id', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        res.data.forEach(element => {
          element.value = element.id;
          element.label = element.name;
          element.isLeaf = false;
          element.createDate = moment(element.createDate).format("YYYY-MM-DD HH:mm:ss")
        });
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*resolve([200, {
      "code": 0,
      "data": [{
        value: '2',
        label: '大楼1',
        type: 2,
        isLeaf: false,
      }, {
        value: '3',
        label: '大楼2',
        type: 2,
        isLeaf: false,
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/community/getFloor').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructure/find_floor_by_build_id', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        res.data.forEach(element => {
          element.value = element.id;
          element.label = element.name;
          element.isLeaf = false;
          element.createDate = moment(element.createDate).format("YYYY-MM-DD HH:mm:ss")
        });
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*
    resolve([200, {
      "code": 0,
      "data": [{
        value: '4',
        label: '1楼',
        type: 3,
        isLeaf: true,
      }, {
        value: '5',
        label: '2楼',
        type: 3,
        isLeaf: true,
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/community/room/all').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructure/find_house_by_floor_id', config).then((res) => {
      res.data = res.data || [];
      resolve([res.status, res]);
    }).catch((err) => {
      reject(err);
    });
    /*
    resolve([200, {
      "code": 0,
      "data": [{
        id:"1",
        name:"test",
        monthCost:10,
        costExpireDay:"2017-09-20",
        otherCost: 0
      }, {
        id:"2",
        name:"test1",
        monthCost:10,
        costExpireDay:"2017-09-20",
        otherCost: 0
      }],
      "message": "操作成功"
    }]);*/
  });
});


mock.onGet('/community/room').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructure/find_house_by_floor_id', config).then((res) => {
      res.data = res.data || [];
      if (res.code == 0) {
        res.data.forEach(element => {
          element.value = element.id;
          element.label = element.name;
          element.isLeaf = true;
          element.createDate = moment(element.createDate).format("YYYY-MM-DD HH:mm:ss")
        });
      }
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
    /*
    resolve([200, {
      "code": 0,
      "data": [{
        id:"1",
        name:"test",
        monthCost:10,
        costExpireDay:"2017-09-20",
        otherCost: 0
      }, {
        id:"2",
        name:"test1",
        monthCost:10,
        costExpireDay:"2017-09-20",
        otherCost: 0
      }],
      "message": "操作成功"
    }]);*/
  });
});

mock.onGet('/community/parents').reply((config) => {
  return new Promise(function (resolve, reject) {
    let id = config.params.id;
    axiosTokenProxy.get('/api/communityStructure/find_parent_id', config).then((res) => {
      let r = [];
      if (res.data) {
        r = res.data.split(",");
      }
      if (r.length > 0 && !r[r.length - 1]) {
        r.pop();
      }
      r.push(id);
      resolve([200, r]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onPost('/community/info/save').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.post('/api/communityInfo', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPut('/community/info/edit').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.put('/api/communityInfo', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onDelete('/community/info/del').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityInfo/' + config.params.id, config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPost('/community/build/save').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.post('/api/communityStructure/saveBuild', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPut('/community/build/edit').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.put('/api/communityStructure/updateBuild', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onDelete('/community/build/del').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityStructure/deleteBuild', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPost('/community/floor/save').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.post('/api/communityStructure/saveFloor', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPut('/community/floor/edit').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.put('/api/communityStructure/updateFloor', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onDelete('/community/floor/del').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityStructure/deleteFloor', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPost('/community/room/save').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.post('/api/communityStructure/saveHouse', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onPut('/community/room/edit').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.put('/api/communityStructure/updateHouse', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});
mock.onDelete('/community/room/del').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityStructure/deleteHouse', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onPost('/community/quick/save').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.post('/api/communityStructure/fastSaveBuild', config).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onGet('/community/getIc').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructureIc/findPage', config).then((res) => {
      res.data = res.data || [];
      if (!res.data.list) {
        res.data.list = [];
      }
      res.data.list = res.data.list.map(ele => {
        ele.createDate = moment(ele.createDate).format("YYYY-MM-DD HH:mm:ss");
        return ele;
      })
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onGet('/community/getCommunityIc').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructureIc/findPageById', config).then((res) => {
      res.data = res.data || [];
      if (!res.data.list) {
        res.data.list = [];
      }
      res.data.list = res.data.list.map(ele => {
        ele.createDate = moment(ele.createDate).format("YYYY-MM-DD HH:mm:ss");
        return ele;
      })
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onGet('/community/getIcInfo').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.get('/api/communityStructureIc/findBinding', config).then((res) => {
      res.data = res.data || [];
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onDelete('/community/delIc').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityStructureIc/del', config).then((res) => {
      res.data = res.data || [];
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

mock.onDelete('/community/delCommunityIc').reply((config) => {
  return new Promise(function (resolve, reject) {
    axiosTokenProxy.delete('/api/communityStructureIc/delRelate', config).then((res) => {
      res.data = res.data || [];
      resolve([200, res]);
    }).catch((err) => {
      reject(err);
    });
  });
});

export default mockAxiosDecorator(mockAxios);
